/*
 * Hidden Equity LLC
 * Author: Maksym Fedyay, Kyiv, mcsimf@gmail.com
 * Date: 29.02.2012
 */

package com.hiddenequity.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DataValidator {

	
	/*
	 * Validate campaign name
	 */
	public static boolean isName(String name){
		boolean result = (!name.trim().equals("") && name != null);
		return result;
	}//isCampaignName
	
	
	/*
	 * Validate developer token
	 */
	public static boolean isDeveloperToken(String token){

		boolean result = (!token.trim().equals("") && token != null && token.length() == 22);

		String reg = "([a-zA-Z0-9]){22}";
		
        Pattern pattern = Pattern.compile(reg, Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
        Matcher matcher = pattern.matcher(token);

        if(!matcher.matches()){
        	result = false;
        } 
		
		return result;
	}
	
	
	/*
	 * Validate phone number
	 */
	public static boolean isPhoneNumber(String number){
		return (!number.trim().equals("") && number != null);
	}
	
	
	/*
	 * Validate Text Ad
	 */
	public static boolean isAdText(String text){
		boolean result = (!text.trim().equals("") && text != null);
		return result;
	}//isAdText
	
	
	
	/*
	 * Validate campaign name
	 */
	public static boolean isAmount(String strAmount, boolean canBeEmpty){ //TODO: add limit from campaign budget
		boolean result = true;
		
		if(canBeEmpty && (strAmount == null || strAmount.equals("")))
			return result;
		
		String reg = "((\\$?(([0-9]{0,1})?\\.[0-9]{1,2}))|(\\$?([1-9]{1}[0-9]{0,2}([0-9]{3})*)(\\.[0-9]{1,2})?))";
		
        Pattern pattern = Pattern.compile(reg, Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
        Matcher matcher = pattern.matcher(strAmount);

        if(!matcher.matches()){
        	result = false;
        	return result;
        }  		
		
        return result;
	}//isCampaignName
	
	
	
	/*
	 * Check amount with campaign budget
	 */
	public static boolean isBidNotExceedBudget(String strAmount, String strLimit){
		boolean result = true;
		
		if(strLimit == null || strLimit.equals(""))
			return result;
		
		if(strAmount == null || strAmount.equals(""))
			return result;
		
		try {
			double amount = Double.parseDouble(strAmount);
			double limit = Double.parseDouble(strLimit);
			if(strLimit != null)
				if(amount>limit)
					result = false;
		} catch (Exception e) {
			
		}
		return result;
	}
	
	
	
	/*
	 * Validating url
	 */
	public static boolean isUrl(String url, boolean fullCheck, boolean canBeEmpty){
		boolean result = true;
		
		if(canBeEmpty && (url == null || url.equals("")))
			return result;
		
		String reg;
		
		if(fullCheck){
			reg = "(https?:\\/\\/([\\-\\w]+\\.)+\\w{2,3}(\\/[%\\-\\w]+(\\.\\w{2,})?)*(([\\w\\-\\.\\?\\\\/+@&#;`~=%!]*)(\\.\\w{2,})?)*\\/?)";
		}else{
			reg = "(([\\-\\w]+\\.)+\\w{2,3}(\\/[%\\-\\w]+(\\.\\w{2,})?)*(([\\w\\-\\.\\?\\\\/+@&#;`~=%!]*)(\\.\\w{2,})?)*\\/?)";
		}
		
		
		
		
        Pattern pattern = Pattern.compile(reg, Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
        Matcher matcher = pattern.matcher(url);

        if(!matcher.matches()){
        	result = false;
        	return result;
        }  	
		//TODO: Implement validation
		return result;		
	}//isUrl
	
	
	
	/*
	 * KeyWord validation
	 */
	public static boolean isKeyword(String keyword){
		
		boolean result = true;
		
		String reg = "[^\\x00]*";
		
        Pattern pattern = Pattern.compile(reg, Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
        Matcher matcher = pattern.matcher(keyword);

        if(!matcher.matches()){
        	result = false;
        	return result;
        } 
		
		//boolean result = (!keyword.trim().equals("") && keyword != null);
		return result;			
	}//isKeyword
	
	
}
